<?php

namespace app\modules\clinical\models;

use Yii;
use yii\web\UploadedFile;

/**
 * This is the model class for table "gene_info".
 *
 * @property integer $pk_id
 * @property integer $pt_id
 * @property integer $rel_id
 * @property string $charge_name
 * @property integer $isSent
 * @property integer $pt_recheck
 * @property string $ck_program
 * @property string $ck_comp
 * @property string $ck_time
 * @property string $rp_time
 * @property string $recheck_time
 * @property string $ck_result
 * @property string $disc_time
 * @property string $disc_result
 * @property string $crt_time
 * @property string $crt_name
 */
class GeneInfo extends \yii\db\ActiveRecord
{
    public $pdfFile;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gene_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pt_id', 'charge_name', 'isSent', 'ck_program', 'ck_comp', 'ck_time'], 'required'],
            [['pt_id', 'rel_id', 'isSent', 'pt_recheck', 'ck_comp'], 'integer'],
            [['ck_time', 'rp_time', 'recheck_time', 'disc_time', 'crt_time', 'update_at', 'delete_at'], 'safe'],
            [['charge_name', 'ck_program'], 'string', 'max' => 30],
            [['ck_result', 'disc_result', 'crt_name', 'update_name'], 'string', 'max' => 255],
            [['pdfFile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pk_id' => 'Pk ID',
            'pt_id' => 'Pt ID',
            'rel_id' => 'Rel ID',
            'charge_name' => '负责人',
            'isSent' => '报告寄送',
            'pt_recheck' => '病人复诊',
            'ck_program' => '送检项目',
            'ck_comp' => '送检公司',
            'ck_time' => '送检时间',
            'rp_time' => '报告时间',
            'recheck_time' => '复诊时间',
            'ck_result' => '检查结果',
            'disc_time' => '讨论时间',
            'disc_result' => '讨论结果',
            'crt_time' => '创建时间',
            'crt_name' => '创建人',
            'pdfFile' => '基因报告文件',
            
            'ppt_id' => '病案号',
            'pt_name' => '患者姓名',
        ];
    }

    public function defaultValue()
    {
        $this->ck_time = date("Y-m-d");
    }

    public function getPatient()
    {
        return $this->hasOne(Patient::className(), ['pk_id' => 'pt_id']);
    }

    public function upload()
    {
        if ($this->validate()) {

            $this->pdfFile->saveAs('../web/upload/pdf/' . $this->pk_id . '.pdf');
            return true;
        } else {

            return false;
        }
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){

                $this->crt_time = date('y-m-d H:i:s');
                $this->crt_name = Yii::$app->user->identity->username;
            }else{
                $this->update_at = date('y-m-d H:i:s');
                $this->update_name = Yii::$app->user->identity->username;
            }
            return true;
        }else{

            return false;
        }
    }

    public static function getParams($type = false, $code = false) {
        $params = [
            'deliver_comp' => array('诺禾致源', '北京金准基因', '溯古生物科技', '麦基诺', '北京航空医学研究所'),

            'isSent' => array('未寄送', '已寄送'),

            'recheck' => array('未完成', '已完成'),
        ];

        if ($code !== false) {
            return ($type) ? $params[$type][$code] : '';
        }

        return ($type) ? $params[$type] : [];
    }
}
